package mdt.action;

import org.tigris.gef.base.Editor;
import org.tigris.gef.base.Globals;
import org.tigris.gef.base.Selection;
import org.tigris.gef.presentation.Fig;
import sun.awt.SunHints;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by xiaobyu on 6/23/2016.
 */
public class CopyAction extends AbstractAction {

    private static final long serialVersionUID = 1L;

    private static final int margin = 50;
    public CopyAction() {
        super("Copy");
    }

    public void actionPerformed(ActionEvent e) {
        Editor ce = Globals.curEditor();
        List<Selection> copiedElements =
                ce.getSelectionManager().getSelections();
        List<Fig> figs = new ArrayList<Fig>();
        Iterator<Selection> copies = copiedElements.iterator();
        while (copies.hasNext()) {
            figs.add(copies.next().getContent());
        }
        Globals.clipBoard = figs;

        Rectangle bounds = null;
        for (Fig f : figs) {
            if (bounds == null) {
                bounds =  f.getBounds();
            } else {
                bounds.add(f.getBounds());
            }
        }

        if (bounds == null) {
            bounds = new Rectangle(0, 0, margin * 2, margin * 2);
        } else {
            if (figs.size() > 0) {
                bounds.width += margin * 2;
                bounds.height += margin * 2;
            }
        }

        BufferedImage image = new BufferedImage(bounds.width, bounds.height,BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = (Graphics2D) image.getGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, SunHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.white);
        g.fillRect(0, 0, image.getWidth(), image.getHeight());
        if (figs.size() > 0) {
            int xClip = margin - bounds.x;
            int yClip = margin - bounds.y;
            g.translate(xClip, yClip);
            for (Fig f : figs) {
                f.paint(g);
            }
        }

        ImageTransferable transferable = new ImageTransferable(image);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(transferable, null);

    }

    static class ImageTransferable implements Transferable
    {
        private Image image;

        public ImageTransferable (Image image)
        {
            this.image = image;
        }

        public Object getTransferData(DataFlavor flavor)
                throws UnsupportedFlavorException
        {
            if (isDataFlavorSupported(flavor))
            {
                return image;
            }
            else
            {
                throw new UnsupportedFlavorException(flavor);
            }
        }

        public boolean isDataFlavorSupported (DataFlavor flavor)
        {
            return flavor == DataFlavor.imageFlavor;
        }

        public DataFlavor[] getTransferDataFlavors ()
        {
            return new DataFlavor[] { DataFlavor.imageFlavor };
        }
    }
}
